package top.ligongzi.hrm.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import top.ligongzi.hrm.doc.CourseDoc;
import top.ligongzi.hrm.dto.CourseDto;
import top.ligongzi.hrm.query.CourseDocQuery;
import top.ligongzi.hrm.service.ICourseService;
import top.ligongzi.hrm.domain.Course;
import top.ligongzi.hrm.query.CourseQuery;
import top.ligongzi.hrm.util.AjaxResult;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ligongzi.hrm.util.PageList;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    public ICourseService courseService;


    /**
     * 保存和修改公用的
     * @param courseDto  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('course:add','course:update')")
    public AjaxResult addOrUpdate(@RequestBody CourseDto courseDto){
        Course course = courseDto.getCourse();
        try {
            if( course.getId()!=null)
                courseService.update(courseDto);
            else
                courseService.insert(courseDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value="/{id}")
    @PreAuthorize("hasAuthority('course:delete')")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            courseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('course:list')")
    public Course get(@PathVariable("id")Long id)
    {
        return courseService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping()
    @PreAuthorize("hasAuthority('course:list')")
    public List<Course> list(){

        return courseService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('course:list')")
    public PageList<Course> json(@RequestBody CourseQuery query)
    {
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        page = courseService.selectPage(page);
        return new PageList<Course>(page.getTotal(),page.getRecords());
    }
    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/queryCourses")
    public PageList<CourseDoc> queryCourses(@RequestBody CourseDocQuery query) //通过依赖feign,feign又依赖common
    {
        try {
            return courseService.queryCourses(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    //===================上下线======================//
    @PostMapping("/online/{id}")
    @PreAuthorize("hasAuthority('course:online')")
    public AjaxResult online(@PathVariable("id") Long id){
        try {
            courseService.onlineOrOffline(id,1);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上线课程失败！"+e.getMessage());
        }
    }
    @PostMapping("/offline/{id}")
    @PreAuthorize("hasAuthority('course:offline')")
    public AjaxResult offline(@PathVariable("id") Long id){
        try {
            courseService.onlineOrOffline(id,0);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下线课程失败！"+e.getMessage());
        }
    }
    //===================上下线======================//
}
